<?php
/**
 * 审批带奖励的群发邮件和单人奖励，审核通过后，真正发送给玩家
 */

require_once('../inc/config.php');
require_once ROOT_PATH .'/utility/class/socket.class.php';
require_once("../stat/class_stat.php");
require_once(ROOT_PATH . "/utility/class/cache.class.php");
require_once("../inc/class/item.class.php");
require_once("../inc/game_setting.php");
require_once("../config/admin_oper_enum.php");
require_once("../config/task_enum.php");
//权限判断
chkpurview('approve_email');

$act = isset($_GET['action']) ? $_GET['action'] : null;
$cache = new Cache('../cache');
$cache->SetCacheId('rs_item.php');	//设置缓存文件名
$item = new Item();
$stat = new Stat();

//审核通过发送
if($act == 'approve')
{
	$id = isset($_POST['id']) ? $_POST['id'] : null;
	$sql = "select oper_value from tb_ad_adminoper_hist where id = $id";
	$opervalue = $db_master->query_first($sql);
	list($title,$profession,$min_level,$max_level,$timing,$content,$rewards) = explode('#', $opervalue['oper_value']);
	$reward = array();
	if(strpos(':', $rewards))
	{
		$rewards = explode(':', $rewards);
		foreach($rewards as $v)
		{
			list($reward['type'][],$reward['value'][]) = explode('.', $v);
		}
	}
	else 
	{
		list($reward['type'],$reward['value']) = explode('.', $rewards);
	}
	
	$sendData = array(
							array('type'=>1,'data'=>$min_level),
							array('type'=>1,'data'=>$max_level),
							array('type'=>1,'data'=>$profession),
							array('type'=>3,'data'=>$title),
							array('type'=>3,'data'=>$content),
							array('type'=>4,'data'=>$reward),
							array('type'=>2,'data'=>$timing),
					);
					
	$protocol = 'email';
		
	$gamesocket = new gamesocket();
	$receive = $gamesocket->send($sendData, $protocol);
	//1：通过审核发送成功 0：失败
	if($receive['retcode'] == '00000000')
	{
		$sql = "update tb_ad_adminoper_hist set oper_status = 0 where id = $id";
		$db_master->query($sql);
		 exit('1');
	} 
	else 
	{
		exit('0');
	}
	
}

//未审核列表
$page = isset($_GET['page'])?$_GET["page"]:1;
$pagesize = 10;
$sql = "SELECT o.id, u.admin_username, o.oper_type, o.oper_value, o.oper_status,
							o.oper_date, o.oper_ip 
			FROM tb_ad_adminoper_hist AS o
			LEFT JOIN tb_ad_adminuser AS u ON u.id=o.admin_id
			WHERE o.oper_status = 1 ";
$count_sql = "SELECT COUNT(*) FROM tb_ad_adminoper_hist where oper_status=1";
$count = $stat->getRecordCount($count_sql);
$admin_oper = $stat->getList($sql, $where='', $page, $order='o.oper_date DESC', $pagesize);
foreach ($admin_oper as $k => $v){
	$admin_oper[$k]['oper_type'] = $admin_oper_enum[$v['oper_type']];
	$admin_oper[$k]['oper_value'] = explain($v['oper_type'], $v['oper_value']);
	$status =$admin_oper[$k]['oper_status'];
	switch ($status)
	{
		case 0:			//发送成功
			$admin_oper[$k]['oper_status'] =$_LANG['operate_success'] ;
			break;
		case 1:			//待审核
			$id = $v['id'];
			$admin_oper[$k]['oper_status'] = "<font color=blue>{$_LANG['wait_approve']}</font>";
			break;
		default:		//发送失败
			$admin_oper[$k]['oper_status'] = "<font color=red>{$_LANG['operate_fail']},{$_LANG['return_code']}:{$v['oper_status']}</font>";
			break;
	}
}
//print_r($admin_oper);exit;
$T->assign('admin_oper',$admin_oper);
$T->assign('page',showpage($page, $pagesize, $count, $url='?'));
$T->display('approve_email.html');

/**
 * 解释操作内容
 * @param Int $oper_type
 * @param String $oper_value
 * @return String
 */
function explain($oper_type, $oper_value){
	global $game_profession, $db, $_LANG;
	$oper_value = explode('#', $oper_value);
	switch ($oper_type){
		case 1://群发邮件
			list($title, $profession, $min_level, $max_level, $timing, $content, $reward) = $oper_value;
			$temp = str_pad(decbin($profession), 5, 0, STR_PAD_LEFT);	//左侧补充0,长度为5
			$temp = str_split(strrev($temp));
			$profession_val = NULL;
			for ($i=1; $i<=count($temp); $i++){
				$profession_val .= $temp[$i-1] ? $game_profession[$i].' ' : NULL;
			}
			$timing = date('Y-m-d H:i:s', intval($timing));
			$reward = reward($reward);
			return "{$_LANG['email_title']}：{$title}<br />
					{$_LANG['profession']}：{$profession_val}<br />
					{$_LANG['level_range']}：{$min_level}--{$max_level}<br />
					{$_LANG['timing_send_time']}：{$timing}<br />
					{$_LANG['email_content']}：{$content}<br />
					{$_LANG['item']}：{$reward}";
		case 2://单人奖励
			list($title, $content, $target_type, $target, $reason, $reward) = $oper_value;
			if ($target_type == 0){//角色
				$target = str_replace(':', ' ', $target);
				$str = $_LANG['nickname'] . $target;
			}elseif ($target_type == 1){//账号
				$target = str_replace(':', ',', $target);
				$sql = "SELECT loginname FROM tb_user WHERE id IN ({$target})";
				$query = $db->query($sql);
				$str = "{$_LANG['loginname']}:";
				while ($row = $db->fetch_array($query)){
					$str .= $row['loginname'].' ';
				}
			}
			$reward = reward($reward);
			return "{$_LANG['email_title']}：{$title}<br />
					{$_LANG['email_content']}：{$content}<br />
					{$_LANG['reward_user']}：{$str}<br />
					{$_LANG['reason']}：{$reason}<br />
					{$_LANG['item']}：{$reward}";
		case 3://禁言
			list($user_id, $reason) = $oper_value;
			$user_id = str_replace(':', ',', $user_id);
			$sql = "SELECT nickname FROM tb_player AS p JOIN tb_user AS u ON u.id=p.user_id WHERE u.id IN ({$user_id})";
			$query = $db->query($sql);
			$str = NULL;
			while ($row = $db->fetch_array($query)){
				$str .= $row['nickname'].' ';
			}
			return "{$_LANG['forbidden_talk']}{$_LANG['user']}：{$str}<br />
					{$_LANG['reason']}：{$reason}";
		case 4://解禁言
			list($user_id, $reason) = $oper_value;
			$user_id = str_replace(':', ',', $user_id);
			$sql = "SELECT nickname FROM tb_player AS p JOIN tb_user AS u ON u.id=p.user_id WHERE u.id IN ({$user_id})";
			$query = $db->query($sql);
			$str = NULL;
			while ($row = $db->fetch_array($query)){
				$str .= $row['nickname'].' ';
			}
			return "{$_LANG['unforbidden_talk']}{$_LANG['user']}：{$str}<br />
					{$_LANG['reason']}：{$reason}";
		case 5://封号
			list($user_id, $reason) = $oper_value;
			$user_id = str_replace(':', ',', $user_id);
			$sql = "SELECT nickname FROM tb_player AS p JOIN tb_user AS u ON u.id=p.user_id WHERE u.id IN ({$user_id})";
			$query = $db->query($sql);
			$str = NULL;
			while ($row = $db->fetch_array($query)){
				$str .= $row['nickname'].' ';
			}
			return "{$_LANG['forbidden_account']}{$_LANG['user']}：{$str}<br />
					{$_LANG['reason']}：{$reason}";
		case 6://解封号
			list($user_id, $reason) = $oper_value;
			$user_id = str_replace(':', ',', $user_id);
			$sql = "SELECT nickname FROM tb_player AS p JOIN tb_user AS u ON u.id=p.user_id WHERE u.id IN ({$user_id})";
			$query = $db->query($sql);
			$str = NULL;
			while ($row = $db->fetch_array($query)){
				$str .= $row['nickname'].' ';
			}
			return "{$_LANG['unforbidden_account']}{$_LANG['user']}：{$str}<br />
					{$_LANG['reason']}：{$reason}";
		case 7://封IP
		case 8://解封IP
		case 9://强制离线
		case 10://修改玩家数据
		case 11://后台登陆玩家账号
			list($T, $name, $description) = $oper_value;
			if ($T == 1){
				$target = $_LANG['loginname'];
			}elseif ($T == 2){
				$target = $_LANG['nickname'];
			}
			return "{$_LANG['user']}：{$target}：{$name}<br />
					{$_LANG['operate_detail']}：{$description}";
		case 13://新增内部用户
			list($user_id) = $oper_value;
			return "{$_LANG['new_inner_user']}user_id：{$user_id}";
		case 14://取消内部用户
			list($user_id) = $oper_value;
			return "{$_LANG['delete_inner_user']}user_id：{$user_id}";
		case 15://指定为新手指导员
			list($user_id, $reason) = $oper_value;
			$user_id = str_replace(':', ',', $user_id);
			$sql = "SELECT nickname FROM tb_player AS p JOIN tb_user AS u ON u.id=p.user_id WHERE u.id IN ({$user_id})";
			$query = $db->query($sql);
			$str = NULL;
			while ($row = $db->fetch_array($query)){
				$str .= $row['nickname'].' ';
			}
			return "{$_LANG['can_newguide']}{$_LANG['user']}：{$str}<br />
							{$_LANG['reason']}：{$reason}";
		case 16://解除新手指导员
			list($user_id, $reason) = $oper_value;
			$user_id = str_replace(':', ',', $user_id);
			$sql = "SELECT nickname FROM tb_player AS p JOIN tb_user AS u ON u.id=p.user_id WHERE u.id IN ({$user_id})";
			$query = $db->query($sql);
			$str = NULL;
			while ($row = $db->fetch_array($query)){
				$str .= $row['nickname'].' ';
			}
			return "{$_LANG['ban_newguide']}{$_LANG['user']}：{$str}<br />
							{$_LANG['reason']}：{$reason}";	
		case 29://新建普通系统公告
			list($content) = $oper_value;
			return "{$_LANG['notice_content']}：{$content}";
		case 30://新建循环系统公告
			list($content, $begin_time, $end_time, $interval) = $oper_value;
			$begin_time = date('Y-m-d H:i:s', $begin_time);
			$end_time = date('Y-m-d H:i:s', $end_time);
			return "{$_LANG['notice_content']}：{$content}<br />
					{$_LANG['start_time']}：{$begin_time}<br />
					{$_LANG['end_time']}：{$end_time}<br />
					{$_LANG['interval']}：{$interval}{$_LANG['minute']}";
		case 31://删除循环系统公告
			list($id, $content, $begin_time, $end_time, $interval) = $oper_value;
			$interval = $interval/60;
			return "{$_LANG['notice']}ID：{$id}<br />
					{$_LANG['notice_content']}：{$content}<br />
					{$_LANG['start_time']}：{$begin_time}<br />
					{$_LANG['end_time']}：{$end_time}<br />
					{$_LANG['interval']}：{$interval}{$_LANG['minute']}";
		case 32://后台开启功能
			list($id) = $oper_value;
			return "{$_LANG['function']}ID：{$id}";
		case 33://后台关闭功能
			list($id) = $oper_value;
			return "{$_LANG['function']}ID：{$id}";
		default:
			return FALSE;
	}
}

/**
 * 解释礼品
 * @param String $reward
 * @return string
 */
function reward($reward){
	global $_LANG;
	if ($reward){
		global $rs_task_gain, $cache, $item;
		$reward_val = explode(':', $reward);
		$str = NULL;
		foreach ($reward_val as $v){
			$temp = explode('.', $v);
			$type = $temp[0];
			$value = $temp[1];
			if ($type == 8200){//道具
				$item_id=$value>>16;//右移16位获得物品ID
				$item_num=$value-($item_id<<16);//任务奖励的值-物品ID左移16位获得物品个数
				if($item_num > 256)
				{
					$isBind = 1;	//绑定
					$item_num = $item_num - 256;
				}
				else 
				{	
					$isBind = 0;	//非绑定
				}
				$rs_gain_item=unserialize($cache->Load());//检查缓存是否存在数据
				if (!isset($rs_gain_item[$item_id])){//如果缓存不存在物品ID,则获取记录
					$cache->Clean();
					$all_item=serialize($item->fetch_item());
					$cache->Write(1,$all_item);
				}
				$rs_gain_item=unserialize($cache->Load());//再次检查缓存是否存在数据
				
				if (isset($rs_gain_item[$item_id])){
					if($isBind)	//绑定
					{
						$str .= $rs_gain_item[$item_id]."({$_LANG['bind']})".'×'.$item_num.',';
					}
					else 
					{
						$str .= $rs_gain_item[$item_id]."({$_LANG['not_bind']})".'×'.$item_num.',';
					}
				}else{
					$str .= "{$_LANG['unknow_item']} id:".$item_id.'×'.$item_num.',';
				}
			}else{//金钱
				$str .= $rs_task_gain[$type].':'.$value.',';
			}
		}
		$str = substr($str, 0 ,strlen($str)-1);
	}else $str = $_LANG['none'];
	return $str;
}
